/// 大整数库的基本测试

/// 测试基本构造函数
test "basic_constructors" {
  let zero = BigInt::zero()
  let one = BigInt::one()
  let from_int = BigInt::from_int(42)
  let negative = BigInt::from_int(-123)
  assert_true(zero.is_zero())
  assert_true(one == BigInt::one())
  assert_true(from_int.to_string() == "42")
  assert_true(negative.to_string() == "-123")
}

/// 测试字符串转换
test "string_conversion" {
  let big_num = BigInt::from_string("123456789012345678901234567890")
  assert_true(big_num.to_string() == "123456789012345678901234567890")
  
  let negative_num = BigInt::from_string("-987654321098765432109876543210")
  assert_true(negative_num.to_string() == "-987654321098765432109876543210")
  
  let zero_str = BigInt::from_string("0")
  assert_true(zero_str.is_zero())
}

/// 测试比较操作
test "comparison_operations" {
  let a = BigInt::from_int(100)
  let b = BigInt::from_int(200)
  let c = BigInt::from_int(100)
  let negative = BigInt::from_int(-50)
  
  assert_true(a < b)
  assert_true(b > a)
  assert_true(a == c)
  assert_true(a >= c)
  assert_true(a <= c)
  assert_true(negative < a)
  assert_true(a > negative)
}

/// 测试加法运算
test "addition" {
  let a = BigInt::from_int(123)
  let b = BigInt::from_int(456)
  let result = a + b
  assert_true(result == BigInt::from_int(579))
  
  // 测试大数加法
  let big_a = BigInt::from_string("123456789012345678901234567890")
  let big_b = BigInt::from_string("987654321098765432109876543210")
  let big_result = big_a + big_b
  assert_true(big_result.to_string() == "1111111110111111111011111111100")
  
  // 测试负数加法
  let neg_a = BigInt::from_int(-100)
  let neg_b = BigInt::from_int(-200)
  let neg_result = neg_a + neg_b
  assert_true(neg_result == BigInt::from_int(-300))
}

/// 测试减法运算
test "subtraction" {
  let a = BigInt::from_int(500)
  let b = BigInt::from_int(200)
  let result = a - b
  assert_true(result == BigInt::from_int(300))
  
  // 测试结果为负数的减法
  let neg_result = b - a
  assert_true(neg_result == BigInt::from_int(-300))
  
  // 测试大数减法
  let big_a = BigInt::from_string("1000000000000000000000000000000")
  let big_b = BigInt::from_string("123456789012345678901234567890")
  let big_result = big_a - big_b
  assert_true(big_result.to_string() == "876543210987654321098765432110")
}

/// 测试乘法运算
test "multiplication" {
  let a = BigInt::from_int(123)
  let b = BigInt::from_int(456)
  let result = a * b
  assert_true(result == BigInt::from_int(56088))
  
  // 测试大数乘法
  let big_a = BigInt::from_string("123456789")
  let big_b = BigInt::from_string("987654321")
  let big_result = big_a * big_b
  assert_true(big_result.to_string() == "121932631137021")
  
  // 测试负数乘法
  let neg_a = BigInt::from_int(-10)
  let pos_b = BigInt::from_int(20)
  let neg_result = neg_a * pos_b
  assert_true(neg_result == BigInt::from_int(-200))
}

/// 测试除法运算
test "division" {
  let a = BigInt::from_int(1000)
  let b = BigInt::from_int(25)
  let quotient = a / b
  assert_true(quotient == BigInt::from_int(40))
  
  // 测试大数除法
  let big_a = BigInt::from_string("123456789012345678901234567890")
  let big_b = BigInt::from_string("123456789")
  let big_quotient = big_a / big_b
  assert_true(big_quotient.to_string() == "1000000001000000001000000001")
}

/// 测试求余运算
test "modulo" {
  let a = BigInt::from_int(1000)
  let b = BigInt::from_int(27)
  let remainder = a % b
  assert_true(remainder == BigInt::from_int(1))
  
  // 测试大数求余
  let big_a = BigInt::from_string("123456789012345678901234567890")
  let big_b = BigInt::from_string("123456789")
  let big_remainder = big_a % big_b
  assert_true(big_remainder.to_string() == "123456789")
}

/// 测试位移操作
test "bit_shifts" {
  let a = BigInt::from_int(100)

  // 左移（乘以2^n）
  let left_shifted = a << 3
  assert_true(left_shifted == BigInt::from_int(800))

  // 右移（除以2^n）
  let right_shifted = left_shifted >> 2
  assert_true(right_shifted == BigInt::from_int(200))
}

/// 测试幂运算
test "power" {
  let base = BigInt::from_int(2)
  let exponent = BigInt::from_int(10)
  let result = base.pow(exponent)
  assert_true(result == BigInt::from_int(1024))
  
  // 测试大数幂运算
  let big_base = BigInt::from_int(3)
  let big_exponent = BigInt::from_int(20)
  let big_result = big_base.pow(big_exponent)
  assert_true(big_result.to_string() == "3486784401")
}

/// 测试模幂运算
test "modular_power" {
  let base = BigInt::from_int(2)
  let exponent = BigInt::from_int(10)
  let modulus = BigInt::from_int(1000)
  let result = base.pow_mod(exponent, modulus)
  assert_true(result == BigInt::from_int(24))
}

/// 测试平方根
test "square_root" {
  let a = BigInt::from_int(100)
  let sqrt_a = a.sqrt()
  assert_true(sqrt_a == BigInt::from_int(10))
  
  let big_a = BigInt::from_string("10000000000000000000000000000000")
  let sqrt_big_a = big_a.sqrt()
  assert_true(sqrt_big_a.to_string() == "100000000000000000")
}

/// 测试最大公约数
test "gcd" {
  let a = BigInt::from_int(48)
  let b = BigInt::from_int(18)
  let gcd_result = a.gcd(b)
  assert_true(gcd_result == BigInt::from_int(6))
  
  // 测试大数GCD
  let big_a = BigInt::from_string("123456789012345678901234567890")
  let big_b = BigInt::from_string("987654321098765432109876543210")
  let big_gcd = big_a.gcd(big_b)
  assert_true(big_gcd == BigInt::from_int(90))
}

/// 测试最小公倍数
test "lcm" {
  let a = BigInt::from_int(12)
  let b = BigInt::from_int(18)
  let lcm_result = a.lcm(b)
  assert_true(lcm_result == BigInt::from_int(36))
}

/// 测试阶乘
test "factorial" {
  let n = BigInt::from_int(5)
  let fact_result = BigInt::factorial(n)
  assert_true(fact_result == BigInt::from_int(120))
  
  // 测试大数阶乘
  let big_n = BigInt::from_int(10)
  let big_fact = BigInt::factorial(big_n)
  assert_true(big_fact.to_string() == "3628800")
}

/// 测试斐波那契数列
test "fibonacci" {
  let n = BigInt::from_int(10)
  let fib_result = BigInt::fibonacci(n)
  assert_true(fib_result == BigInt::from_int(55))

  // 测试大斐波那契数
  let big_n = BigInt::from_int(20)
  let big_fib = BigInt::fibonacci(big_n)
  assert_true(big_fib.to_string() == "6765")
}

/// 测试组合数
test "combination" {
  let n = BigInt::from_int(10)
  let k = BigInt::from_int(3)
  let comb_result = BigInt::combination(n, k)
  assert_true(comb_result == BigInt::from_int(120))
}

/// 测试奇偶性判断
test "odd_even" {
  let odd = BigInt::from_int(123)
  let even = BigInt::from_int(124)
  assert_true(odd.is_odd())
  assert_true(not(odd.is_even()))
  assert_true(even.is_even())
  assert_true(not(even.is_odd()))
}

/// 测试绝对值和取负
test "abs_and_negate" {
  let positive = BigInt::from_int(123)
  let negative = BigInt::from_int(-123)
  assert_true(positive.abs() == positive)
  assert_true(negative.abs() == positive)
  assert_true(positive.negate() == negative)
  assert_true(negative.negate() == positive)
}

/// 测试边界情况
test "edge_cases" {
  let zero = BigInt::zero()
  let one = BigInt::one()

  // 零的各种运算
  assert_true(zero + one == one)
  assert_true(zero * one == zero)

  // 一的各种运算
  assert_true(one + zero == one)
  assert_true(one * one == one)
}

test "edge_cases" {
  let zero = BigInt::zero()
  let one = BigInt::one()

  // 零的各种运算
  assert_true(zero + one == one)
  assert_true(zero * one == zero)

  // 一的各种运算  
  assert_true(one + zero == one)
  assert_true(one * one == one)
}

test "edge_cases" {
  let zero = BigInt::zero()
  let one = BigInt::one()

  // 零的各种运算
  assert_true(zero + one == one)
  assert_true(zero * one == zero)

  // 一的各种运算
  assert_true(one + zero == one)
  assert_true(one * one == one)
}

test "edge_cases" {
  let zero = BigInt::zero()
  let one = BigInt::one()

  // 零的各种运算
  assert_true(zero + one == one)
  assert_true(zero * one == zero)

  // 一的各种运算
  assert_true(one + zero == one)
  assert_true(one * one == one)
}
